/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.util;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import mediautil.gen.Log;
import mediautil.image.jpeg.LLJTran;

/** Uses this library to transform images:
 * http://mediachest.sourceforge.net/mediautil/
 * Copyright (C) 1999-2005 Dmitriy Rogatkin, Suresh Mahalingam.  All rights reserved.
 *
 * @author petros
 *
 */
public class ImageUtil {

	public final static int ROT_90 = LLJTran.ROT_90;
	public final static int ROT_180 = LLJTran.ROT_180;
	public final static int ROT_270 = LLJTran.ROT_270;
	
	public static BufferedImage loadImage(String ref) {  
        BufferedImage bimg = null;  
        try {  
            bimg = ImageIO.read(new File(ref));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return bimg;  
    }  
	
	/** Rotates pImg using the pRotAngle.
	 * @param pImg
	 * @param pRotAngle
	 * @throws Exception
	 */
	public static void rotateImage(String pImg, int pRotAngle) throws Exception {
		
		// Raise the Debug Level which is normally LEVEL_INFO. Only Warning
        // messages will be printed by MediaUtil.
        Log.debugLevel = Log.LEVEL_WARNING;

        // 1. Initialize LLJTran and Read the entire Image including Appx markers
        LLJTran llj = new LLJTran(new File(pImg));
        // If you pass the 2nd parameter as false, Exif information is not
        // loaded and hence will not be written.
        llj.read(LLJTran.READ_ALL, true);

        // 2. Transform the image using default options along with
        // transformation of the Orientation tags. Try other combinations of
        // LLJTran_XFORM.. flags. Use a jpeg with partial MCU (partialMCU.jpg)
        // for testing LLJTran.XFORM_TRIM and LLJTran.XFORM_ADJUST_EDGES
        
        int options = LLJTran.OPT_DEFAULTS | LLJTran.OPT_XFORM_ORIENTATION;
        llj.transform(pRotAngle, options);
        
        // 4. Save the Image which is already transformed as specified by the
        //    input transformation in Step 2, along with the Exif header.
        OutputStream out = new BufferedOutputStream(
                    new FileOutputStream(pImg));
        llj.save(out, LLJTran.OPT_DEFAULTS);
        out.close();

        // Cleanup
        llj.freeMemory();
        llj = null;
	}
	
	public static void main(String[] args) {
		try {
			ImageUtil.rotateImage("/Users/petros/tmp/rotit/IMG_0191.JPG", ROT_270);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
